package com.lin.zoo.dao;

import com.lin.zoo.dao.Interface.IDAnimal;
import com.lin.zoo.util.DataBase;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class AnimalDao implements IDAnimal {

    private Connection con;
    private PreparedStatement state;
    @Override
    //删除动物信息
    public void delAnimal(Object id){
        try{
            con= DataBase.getCon();
            state = con.prepareStatement("DELETE FROM animal WHERE id=?");//删除所选的动物信息
            state.setObject(1,id);
            state.executeUpdate();
            DataBase.close(state, con);
        } catch (SQLException | ClassNotFoundException ex) {
            System.out.println("数据删除错误"+this.getClass().getName());
        }
    }
    @Override
    //设置动物名称
    public void setName(Object id,String name){
        try{
            con=DataBase.getCon();
            state = con.prepareStatement("update animal set name=? WHERE id=?");//将修改的数据返回数据库
            state.setObject(1,name);
            state.setObject(2,id);
            state.executeUpdate();
            DataBase.close(state, con);

        }catch (SQLException | ClassNotFoundException ex) {
            System.out.println("数据修改错误"+this.getClass().getName());
        }
    }
    @Override
    //设置动物类型
    public void setType(Object id,String type){
        try{
            con=DataBase.getCon();
            state = con.prepareStatement("update animal set type=? WHERE id=?");//将修改的数据返回数据库
            state.setObject(1,type);
            state.setObject(2,id);
            state.executeUpdate();
            DataBase.close(state, con);

        }catch (SQLException | ClassNotFoundException ex) {
            System.out.println("数据修改错误"+this.getClass().getName());
        }
    }
    @Override
    //设置动物介绍
    public void setIntroduce(Object id,String introduce){
        try{
            con=DataBase.getCon();
            state = con.prepareStatement("update animal set introduction=? WHERE id=?");//将修改的数据返回数据库
            state.setObject(1,introduce);
            state.setObject(2,id);
            state.executeUpdate();
            DataBase.close(state, con);

        }catch (SQLException | ClassNotFoundException ex) {
            System.out.println("数据修改错误"+this.getClass().getName());
        }
    }
    @Override
    //设置动物年龄
    public void setAge(Object id,String age){
        try{
            con=DataBase.getCon();
            state = con.prepareStatement("update animal set age=? WHERE id=?");//将修改的数据返回数据库
            state.setObject(1,age);
            state.setObject(2,id);
            state.executeUpdate();
            DataBase.close(state, con);

        }catch (SQLException | ClassNotFoundException ex) {
            System.out.println("数据修改错误"+this.getClass().getName());
        }
    }
    @Override
    //设置动物健康状况
    public void setHealth(Object id,String health){
        try{
            con=DataBase.getCon();
            state = con.prepareStatement("update animal set healthstate=? WHERE id=?");//将修改的数据返回数据库
            state.setObject(1,health);
            state.setObject(2,id);
            state.executeUpdate();
            DataBase.close(state, con);

        }catch (SQLException | ClassNotFoundException ex) {
            System.out.println("数据修改错误"+this.getClass().getName());
        }
    }
    @Override
    //设置所属园区
    public void setPark(Object id,String parkId){
        try{
            con=DataBase.getCon();
            state = con.prepareStatement("update animal set park_id=? WHERE id=?");//将修改的数据返回数据库
            state.setObject(1,parkId);
            state.setObject(2,id);
            state.executeUpdate();
            DataBase.close(state, con);

        }catch (SQLException | ClassNotFoundException ex) {
            System.out.println("数据修改错误"+this.getClass().getName());
        }
    }
}
